package json;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import modelo.Cupon;
import modelo.Oferta;
import modelo.Oferta;
import modelo.Producto;
import modelo.Venta;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JacksonWriter implements IWriter {

	private final Logger kLog = LoggerFactory.getLogger(JacksonWriter.class);

	private String productsFile;
	private String offerProductsFile;
	private String offerSellFile;
	private String sellsFile;
	private String cuponFile;
	
	private ObjectWriter jsonWriter;


	public JacksonWriter() {
		this.productsFile = null;
		this.offerProductsFile = null;
		this.offerSellFile =null;
		this.sellsFile =null;
		this.cuponFile = null;
		
		ObjectMapper jsonMapper = new ObjectMapper();
		jsonWriter = jsonMapper.writer().withDefaultPrettyPrinter();

	}
	
	public void writeProducts(List<Producto> products) throws IOException {
		kLog.debug("Writing Products Json to file");
		FileWriter fileWriter = new FileWriter(productsFile);

		fileWriter.write(jsonWriter.writeValueAsString(products));

		fileWriter.close();
		kLog.debug("Products Json file created.");		
	}

	public void writeOfferProducts(List<Oferta> Ofertas)
			throws IOException {
		kLog.debug("Writing Offer Products Json to file");
		FileWriter fileWriter = new FileWriter(offerProductsFile);

		fileWriter.write(jsonWriter.writeValueAsString(Ofertas));

		fileWriter.close();
		kLog.debug("Offer Products Json file created.");	
		
	}

	public void writeOfferSell(List<Oferta> offerSell) throws IOException {
		kLog.debug("Writing Offer Sell Json to file");
		FileWriter fileWriter = new FileWriter(offerSellFile);
		
		fileWriter.write(jsonWriter.writeValueAsString(offerSell));

		fileWriter.close();
		kLog.debug("Offer Sell Json file created.");	
		
	}
	
	public void writeSells(List<Venta> sells) throws IOException {
		kLog.debug("Writing Sells Json to file");
		FileWriter fileWriter = new FileWriter(sellsFile);
		
		fileWriter.write(jsonWriter.writeValueAsString(sells));

		fileWriter.close();
		kLog.debug("Sells Json file created.");	
		
	}

	@Override
	public void writeCupons(List<Cupon> cupons) throws IOException {
		kLog.debug("Writing Cupon Json to file");
		FileWriter fileWriter = new FileWriter(cuponFile);
		
		fileWriter.write(jsonWriter.writeValueAsString(cupons));

		fileWriter.close();
		kLog.debug("Cupon Json file created.");	
				
	}

	public void setProductsFile(String productsFile) {
		this.productsFile = productsFile;
	}

	public void setOfferProductsFile(String offerProductsFile) {
		this.offerProductsFile = offerProductsFile;
	}

	public void setOfferSellFile(String offerSellFile) {
		this.offerSellFile = offerSellFile;
	}

	public void setSellsFile(String sellsFile) {
		this.sellsFile = sellsFile;
	}

	public void setCuponFile(String cuponFile) {
		this.cuponFile = cuponFile;
	}

	
	

}
